from rest_framework import serializers
from goods.models import SKU,GoodsCategory,SPU,SPUSpecification,SpecificationOption,SKUSpecification
from django.db import transaction

# 4.1商品sku的规格信息,序列化器
class SKUSpecificationSerializer(serializers.Serializer):
    spec_id = serializers.IntegerField()
    option_id = serializers.IntegerField()

# 4.2获取sku信息的序列化器
class SKUSerializer(serializers.ModelSerializer):
    # 1.重写spu,spu_id
    spu = serializers.StringRelatedField(read_only=True)
    spu_id = serializers.IntegerField()

    # 2.重写category,category_id
    category = serializers.StringRelatedField(read_only=True)
    category_id = serializers.IntegerField()

    # 3.specs
    specs = SKUSpecificationSerializer(read_only=True,many=True)

    class Meta:
        model = SKU
        fields = "__all__"

    # 3.重写create方法,保存sku和对应的规格
    @transaction.atomic
    def create(self,validated_data):
        # TODO 设置保存点
        sid = transaction.savepoint()

        try:
            # 1.创建sku对象入库
            sku = SKU.objects.create(**validated_data)
            # 2.创建规格信息入库
            specs = self.context["request"].data["specs"]
            for spec_dict in specs:
                SKUSpecification.objects.create(sku_id=sku.id,spec_id=spec_dict["spec_id"],option_id=spec_dict["option_id"])
        except Exception as e:
            transaction.savepoint_rollback(sid) #TODO 回滚
            raise serializers.ValidationError("保存失败")
        else:
            # 3.返回响应
            transaction.savepoint_commit(sid)   # TODO 提交
            return sku

    #　４.重写update方法,更新sku中的规格
    @transaction.atomic
    def update(self, instance, validated_data):
        # TODO 设置保存点
        sid = transaction.savepoint()

        try:
            # 1.创建sku对象入库
            SKU.objects.filter(id=instance.id).update(**validated_data)

            # 1.删除以前所有的规格
            [spec.delete() for spec in instance.specs.all()]


            # 2.创建规格信息入库
            specs = self.context["request"].data["specs"]
            for spec_dict in specs:
                SKUSpecification.objects.filter(sku_id=instance.id,spec_id=spec_dict["spec_id"]).update(
                                                option_id=spec_dict["option_id"])
        except Exception as e:
            transaction.savepoint_rollback(sid)  # TODO 回滚
            raise serializers.ValidationError("保存失败")
        else:
            # 3.返回响应
            transaction.savepoint_commit(sid)  # TODO 提交
            return SKU.objects.get(id=instance.id)

# 5.获取sku的三级分类的序列化器
class SKUCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = ("id","name")

# 6.获取spu信息序列化器
class GoodSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SPU
        fields = ("id","name")

# 商品规格的选项
class SpecificationOption(serializers.ModelSerializer):
    class Meta:
        model = SpecificationOption
        fields = ("id","value")

# 7.sku的商品规格序列化器
class SKUGoodSpecsSerializer(serializers.ModelSerializer):
    # 1.重写spu
    spu = serializers.StringRelatedField(read_only=True)
    spu_id = serializers.IntegerField()

    # 2.重写option
    # specs = serializers.XXXX
    options = SpecificationOption(read_only=True,many=True)

    class Meta:
        model = SPUSpecification
        fields = "__all__"



